<?php

namespace app\models;

use Yii;

/**
 * This is the model class for table "socialwork".
 *
 * @property integer $id
 * @property string $stu_id
 * @property string $stu_name
 * @property string $organization
 * @property string $position
 * @property string $start_time
 * @property string $end_time
 * @property string $remark
 */
class SocialworkModel extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'socialwork';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['stu_id', 'stu_name', 'organization', 'position', 'start_time', 'end_time'], 'required'],
            [['stu_id'], 'string', 'max' => 20],
            [['stu_name', 'position'], 'string', 'max' => 50],
            [['organization', 'start_time', 'end_time', 'remark'], 'string', 'max' => 200],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'stu_id' => '学号',
            'stu_name' => '姓名',
            'organization' => '组织',
            'position' => '职务',
            'start_time' => '开始日期',
            'end_time' => '结束日期',
            'remark' => '备注',
        ];
    }


    /**
     * 保存导入excel文件内容
     * @param $data  传入文件内容的数组
     * @return bool  保存成功返回true
     *
     */
    public static function SaveImport($data){

        try{


            foreach ($data as $value){
                $person=new SocialworkModel();
                $person->stu_id=$value['学号'].'';//学号要转化为字符型才能通过验证
                $person->stu_name=$value['姓名'].'';
                $person->organization=$value['组织'].'';
                $person-> position=$value['职务'];
                $person->start_time=$value['开始日期'].'';
                $person->end_time=$value['结束日期'].'';
                $person->remark=$value['备注'].'';


                $person->save();
            }
            return true;

        }catch (\Exception $e){
            throw $e;
        }

    }


}
